package leetcode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shuowu on 2018/3/12.
 */
public class EmployeeImportance {

    class Employee {
        // It's the unique id of each node;
        // unique id of this employee
        public int id;
        // the importance value of this employee
        public int importance;
        // the id of direct subordinates
        public List<Integer> subordinates;
    };

    Map<Integer, Employee> map = new HashMap<>();

    public int getImportance(List<Employee> employees, int id) {
        //也就是循环取所有下级的importance
        if (employees == null || employees.size() <= 0) {
            return 0;
        }
        map.clear();
        int sum = 0;
        //id 到 employee的映射
        for (Employee cur : employees) {
            map.put(cur.id, cur);
        }
        Employee info = map.get(id);
        return getCurImportance(info);
    }

    public int getCurImportance(Employee employee) {
        if (employee == null) {
            return 0;
        }
        int sum = employee.importance;
        for (Integer subId : employee.subordinates) {
            sum += getCurImportance(map.get(subId));
        }
        return sum;
    }
}
